Range Quantifiers in Regular Expressions
1. Exact Quantifier: {n}
The exact quantifier {n}
specifies that the preceding element must appear exactly n
times. This quantifier is useful when you need to match a specific number of occurrences of a character or group.
Example:
Pattern: a{3}
Matches: "aaa"
Explanation: The pattern a{3}
matches exactly three 'a' characters.
2. Minimum Quantifier: {n,}
The minimum quantifier {n,}
specifies that the preceding element must appear at least n
times. This quantifier is useful when you need to match a minimum number of occurrences of a character or group.
Example:
Pattern: a{2,}
Matches: "aa", "aaa", "aaaa", etc.
Explanation: The pattern a{2,}
matches two or more 'a' characters.
3. Range Quantifier: {n,m}
The range quantifier {n,m}
specifies that the preceding element must appear at least n
times and at most m
times. This quantifier is useful when you need to match a range of occurrences of a character or group.
Example:
Pattern: a{2,4}
Matches: "aa", "aaa", "aaaa"
Explanation: The pattern a{2,4}
matches between two and four 'a' characters.